#include "WThreadPool.h"
#include <iostream>

using namespace std;

int addNum(int num1, int num2)
{
    return num1 + num2;
}

class Test
{
public:
    int test(int num)
    {
        return num;
    }

    int testThread()
    {
        // WThreadPool::globalInstance()->concurrentRun(&Test::test, this, 100);
        auto testFuture = WThreadPool::globalInstance()->concurrentRun(&Test::test, this, 100);
        cout << "get testFuture value: " << testFuture.get() << endl;
        return 0;
    }
};

int main(int argc, char *argv[])
{
    WThreadPool::globalInstance()->setMaxThreadNum(20);

    cout << "the main thread  pid: " << this_thread::get_id() << endl;
    for (int i = 0; i < 20; i++)
    {
        WThreadPool::globalInstance()->concurrentRun([=] (int index) {
           this_thread::sleep_for(chrono::milliseconds(index + 10));
           if (index == 19)
           {
               this_thread::sleep_for(chrono::milliseconds(3000));
           }
           cout << "the sub thread  pid: " << this_thread::get_id() << " ,index is " << index << endl;
           fflush(stdout);
           if (index == 19)
           {
               this_thread::sleep_for(chrono::milliseconds(3000));
           }

        }, i);
    }

    bool flag = WThreadPool::globalInstance()->waitForDone(-1);
    cout << "get here to wait: " << flag << endl;

    // WThreadPool::globalInstance()->concurrentRun(addNum, 100, 4);
    auto addFuture = WThreadPool::globalInstance()->concurrentRun(addNum, 100, 4);
    cout << "get addFuture value: " << addFuture.get() << endl;

    Test test;
    test.testThread();

    this_thread::sleep_for(chrono::milliseconds(1000));

    getchar();
    return 0;
}
